
function FillUpContextCommands()

	EpContextPanel.Options = { };

	if( ClientVars["Ragdolled"] ) then
	
		table.insert( EpContextPanel.Options, { Text = "Get up", Cmd = "rp_getup" } );
	
	else
	
		table.insert( EpContextPanel.Options, { Text = "Passout", Cmd = "rp_passout" } );
	
	end

end

function OpenEpidemicContextMenu()

	EpContextPanel = CreateBPanel( nil, ScrW() * .5, ScrH() * .5, 0, 0 );
	
	FillUpContextCommands();
	
	surface.SetFont( "ActionMenuOption" );
	
	local w = 0;
	local h = 0;
	
	--do two loops?	
	
	for k, v in pairs( EpContextPanel.Options ) do
	
		local tw = surface.GetTextSize( v.Text );
		
		if( tw > w ) then
		
			w = tw;
		
		end
		
		h = h + 18;
	
	end
	
	EpContextPanel:SetPos( ScrW() * .5 - w * .5 - 6, ScrH() * .5 - h * .5 );
	EpContextPanel:SetSize( w + 12, h + 8 );
	
	--we do this because we need to know how wide this panel is, which the first loop determines
	
	for k, v in pairs( EpContextPanel.Options ) do
	
		local tw = surface.GetTextSize( string.gsub( v.Text, " ", "   " ) );
		
		local func = function()
		
			if( not LocalPlayer():Alive() ) then return; end
		
			RunConsoleCommand( v.Cmd, "" );
		
		end
		
		EpContextPanel:AddLink( string.gsub( v.Text, " ", "   " ), "ActionMenuOption", w * .5 - tw * .5 + 6, ( k - 1 ) * 15 + 5, Color( 150, 0, 0, 255 ), func, Color( 255, 255, 255, 255 ) );
	
	end
	
	gui.EnableScreenClicker( true );

end

function CloseEpidemicContextMenu()

	EpContextPanel:Remove();
	EpContextPanel = nil;
	
	HideMouse();

end

GModContext = false;

function GM:OnContextMenuOpen()
 
 	if( not LocalPlayer():Alive() ) then return; end
 	if( PlayerMenuPanel:IsVisible() ) then return; end
 	if( BlackScreenOn or OpeningScreen.HUDOverride ) then return; end
 	if( not hook.Call( "SpawnMenuOpen", GAMEMODE ) ) then return; end
    
    if( LocalPlayer():GetActiveWeapon():GetClass() == "gmod_tool" ) then
    
	    if( g_ContextMenu ) then 

	    	g_ContextMenu:Open();
	    	GModContext = true;
	
	    end
	    
	else
	
		OpenEpidemicContextMenu();
		GModContext = false;
	
	end
       
end

function GM:OnContextMenuClose()

	if( GModContext ) then

		if( g_ContextMenu ) then 

			g_ContextMenu:Close();
	
		end
	
	else
	
		CloseEpidemicContextMenu();
	
	end   
	
end